<template>
  <div class="login">
    <div class="container">
      <img src="../assets/avatar.jpg" alt class="avatar" />
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input
            @focus="headerFocus('username')"
            prefix-icon="icon-user"
            placeholder="请输入用户名"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            @focus="headerFocus('password')"
            prefix-icon="icon-key"
            placeholder="请输入密码"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="userLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/apis/user.js";
export default {
  data() {
    return {
      loginForm: {
        username: "10086",
        password: "123",
      },
      // 添加数据的验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "请输入3-8位的密码", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 用户登录
    userLogin() {
      // let res = await login(this.loginForm);
      // console.log(res);
      // 先进行用户输入的验证==二次验证,只有通过验证才会进行登录的提交
      this.$refs.loginForm.validate(async (valid, obj) => {
        // valid就是本次根据rules规则检验用户输入数据的结果
        console.log(valid, obj);
        if (valid) {
          let res = await login(this.loginForm);
          console.log(res);
          if (res.data.statusCode == 401) {
            this.$message({
              message: res.data.message,
              type: "warning",
            });
          } else {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            // 存储当前用户的登录状态
            localStorage.setItem("heima_toutiao_houtai", res.data.data.token);
            // 跳转到后台首页
            this.$router.push({ name: "index" });
          }
        } else {
          this.$message({
            message: "数据输入不合法",
            type: "warning",
          });
          return false;
        }
      });
    },
    headerFocus(pn) {
      this.$refs.loginForm.clearValidate(pn);
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>